<template>
  <view class="ranking-container">
    <view class="main-card">
      <!-- 标题区域 -->
      <view class="header-section">
        <view class="header-content">
          <view class="header-text">
            <h1>项目排行榜</h1>
            <p>实时展示当前最受欢迎的项目排名</p>
          </view>
          <button class="refresh-btn" @click="refreshRanking">
            <i class="fas fa-sync-alt"></i> 刷新数据
          </button>
        </view>
      </view>

      <!-- 筛选区域 -->
      <view class="filters-section">
        <view class="filters-container">
          <view class="filter-group">
            <i class="fas fa-search filter-icon"></i>
            <input 
              v-model="searchQuery"
              type="text" 
              placeholder="搜索项目名称..." 
              class="filter-input"
              @keyup.enter="fetchRanking">
          </view>
          
          <view class="filter-group">
            <select
              v-model="selectedCategory"
              class="filter-select"
              @change="fetchRanking">
              <option value="">全部</option>
              <option value="1">大一</option>
              <option value="2">大二</option>
              <option value="3">大三</option>
            </select>
            <i class="fas fa-chevron-down select-arrow"></i>
          </view>
          
          <view class="filter-group">
            <select
              v-model="sortType"
              class="filter-select"
              @change="sortItems">
              <option value="score-desc">评分 (高到低)</option>
              <option value="score-asc">评分 (低到高)</option>
            </select>
            <i class="fas fa-sort select-arrow"></i>
          </view>
        </view>
      </view>

      <!-- 排行榜内容 -->
      <view class="ranking-content">
        <!-- 加载状态 -->
        <view v-if="loading" class="loading-state">
          <view class="spinner"></view>
        </view>
        
        <!-- 空状态 -->
        <view v-else-if="filteredItems.length === 0" class="empty-state">
          <view class="empty-icon">
            <i class="fas fa-chart-bar"></i>
          </view>
          <h3 class="empty-title">暂无数据</h3>
          <p class="empty-text">没有找到符合条件的项目，请尝试调整筛选条件</p>
        </view>
        
        <!-- 排行榜列表 -->
        <view v-else class="ranking-list">
          <view 
            v-for="(item, index) in paginatedItems" 
            :key="item.projectId"
            :class="['ranking-item', 
                     index === 0 ? 'rank-1' : 
                     index === 1 ? 'rank-2' : 
                     index === 2 ? 'rank-3' : 'rank-other']">
            <view class="item-header" @click="toggleItem(item)">
              <view class="rank-number">{{ index + 1 }}</view>
              
              <view class="item-info">
                <h3 class="item-title">{{ item.name }}</h3>
                <view class="item-meta">
                  <view class="meta-item">
                    <i class="fas fa-user"></i>
                    <span>{{ item.projectMember }}</span>
                  </view>
                </view>
              </view>
              
              <view class="item-score">
                {{ item.score.toFixed(0) }}
                <span class="score-label">/ 100</span>
              </view>
              
              <view 
                class="toggle-btn" 
                :class="{ expanded: item.expanded }"
                @click.stop="toggleItem(item)">
                <i class="fas fa-chevron-down"></i>
              </view>
            </view>
            
            <view class="item-details" :class="{ expanded: item.expanded }">
              <p class="item-description">{{ item.description }}</p>
              
              <view class="item-tags">
                <span class="tag">{{ getCategoryName(item.voteId) }}</span>
              </view>
            </view>
          </view>
        </view>
        
        <!-- 分页控制 -->
        <view class="pagination-section">
          <view class="pagination-info">
            显示 {{ (currentPage - 1) * pageSize + 1 }}-{{ Math.min(currentPage * pageSize, totalItems) }} 条，共 {{ totalItems }} 条
          </view>
          
          <view class="pagination-controls">
            <button 
              class="pagination-btn"
              :class="{ 'disabled': currentPage === 1 }"
              @click="prevPage">
              <i class="fas fa-chevron-left"></i>
            </button>
            
            <button 
              v-for="page in visiblePages" 
              :key="page"
              class="pagination-btn"
              :class="{ 'active': page === currentPage }"
              @click="goToPage(page)">
              {{ page }}
            </button>
            
            <button 
              class="pagination-btn"
              :class="{ 'disabled': currentPage === totalPages }"
              @click="nextPage">
              <i class="fas fa-chevron-right"></i>
            </button>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { getAllProject } from '@/api/score.js';

export default {
  name: 'CollapsibleRanking',
  data() {
    return {
      // 筛选条件
      searchQuery: '',
      selectedCategory: '',
      sortType: 'score-desc',
      
      // 分页控制
      currentPage: 1,
      pageSize: 6, // 每页显示6条数据
      totalItems: 0,
      totalPages: 0,
      
      // 数据
      allItems: [], // 存储所有项目数据
      filteredItems: [], // 存储筛选后的项目
      paginatedItems: [], // 当前页显示的项目
      loading: false,
    };
  },
  computed: {
    // 计算可见的页码范围
    visiblePages() {
      let startPage = Math.max(1, this.currentPage - 2);
      let endPage = Math.min(this.totalPages, startPage + 4);
      
      if (endPage - startPage < 4 && startPage > 1) {
        startPage = Math.max(1, endPage - 4);
      }
      
      return Array.from({ length: endPage - startPage + 1 }, (_, i) => startPage + i);
    }
  },
  mounted() {
    // 组件挂载后加载数据
    this.fetchRanking();
  },
  onShow() {
    // 页面展示时更新排行榜数据
    this.fetchRanking();
  },
  methods: {
    // 分类名称映射
    getCategoryName(voteId) {
      const categories = {
        '1': '大一',
        '2': '大二',
        '3': '大三'
      };
      return categories[voteId] || '其他';
    },
    
    // 从 API 获取排行榜数据
    async fetchRanking() {
      try {
        this.loading = true;
        const response = await getAllProject();//////////
        
        // 添加展开状态
        const itemsWithExpanded = response.data.map(item => ({
          ...item,
          expanded: false,
          // 确保voteId是字符串类型以便比较
          voteId: item.voteId ? item.voteId.toString() : ''
        }));
        
        this.allItems = itemsWithExpanded;
        this.filterItems();
        
      } catch (error) {
        console.error('获取排行榜数据失败:', error);
      } finally {
        this.loading = false;
      }
    },
    
    // 筛选项目
    filterItems() {
      this.filteredItems = this.allItems.filter(item => {
        // 分类筛选
        if (this.selectedCategory && item.voteId !== this.selectedCategory) {
          return false;
        }
        
        // 搜索筛选
        if (this.searchQuery) {
          const searchLower = this.searchQuery.toLowerCase();
          return item.name.toLowerCase().includes(searchLower) ||
                 item.description.toLowerCase().includes(searchLower);
        }
        
        return true;
      });
      
      // 更新分页信息
      this.totalItems = this.filteredItems.length;
      this.totalPages = Math.ceil(this.totalItems / this.pageSize);
      
      // 排序
      this.sortItems();
      
      // 应用分页
      this.applyPagination();
    },
    
    // 对项目进行排序
    sortItems() {
      if (this.sortType === 'score-desc') {
        this.filteredItems.sort((a, b) => b.score - a.score);
      } else if (this.sortType === 'score-asc') {
        this.filteredItems.sort((a, b) => a.score - b.score);
      }
      
      // 排序后重新应用分页
      this.applyPagination();
    },
    
    // 应用分页
    applyPagination() {
      const startIndex = (this.currentPage - 1) * this.pageSize;
      this.paginatedItems = this.filteredItems.slice(
        startIndex, 
        startIndex + this.pageSize
      );
    },
    
    // 切换项目展开状态
    toggleItem(item) {
      item.expanded = !item.expanded;
    },
    
    // 刷新排行榜
    refreshRanking() {
      this.currentPage = 1;
      this.fetchRanking();
    },
    
    // 上一页
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
        this.applyPagination();
      }
    },
    
    // 下一页
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
        this.applyPagination();
      }
    },
    
    // 跳转到指定页
    goToPage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.currentPage = page;
        this.applyPagination();
      }
    }
  }
};
</script>

<style scoped>
/* 基础样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

body {
  background: linear-gradient(135deg, #f0f4ff, #f9f7ff);
  min-height: 100vh;
  padding: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.ranking-container {
  max-width: 1200px;
  width: 100%;
}

.main-card {
  background: white;
  border-radius: 20px;
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.4s ease;
}

.main-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
}

/* 标题区域 */
.header-section {
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  padding: 28px 32px;
  position: relative;
  overflow: hidden;
}

.header-section::before {
  content: "";
  position: absolute;
  top: -50%;
  right: -50%;
  width: 200px;
  height: 200px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  z-index: 1;
}

.header-section::after {
  content: "";
  position: absolute;
  bottom: -30px;
  left: -30px;
  width: 100px;
  height: 100px;
  background: rgba(255, 255, 255, 0.07);
  border-radius: 50%;
  z-index: 1;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  z-index: 2;
}

.header-text h1 {
  font-size: 2.5rem;
  font-weight: 800;
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  letter-spacing: -0.5px;
  margin-bottom: 8px;
}

.header-text p {
  font-size: 1.1rem;
  color: rgba(255, 255, 255, 0.9);
}

.refresh-btn {
  padding: 14px 24px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border-radius: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  border: none;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
}

.refresh-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

/* 筛选区域 */
.filters-section {
  padding: 24px 32px;
  background: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
}

.filters-container {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.filter-group {
  position: relative;
  flex: 1;
  min-width: 200px;
}

.filter-input {
  width: 100%;
  padding: 16px 20px 16px 48px;
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: white;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.03);
}

.filter-input:focus {
  outline: none;
  border-color: #6a11cb;
  box-shadow: 0 0 0 3px rgba(106, 17, 203, 0.1);
}

.filter-icon {
  position: absolute;
  left: 18px;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
  font-size: 1.1rem;
}

.filter-select {
  width: 100%;
  padding: 16px 48px 16px 20px;
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: white;
  appearance: none;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.03);
}

.filter-select:focus {
  outline: none;
  border-color: #6a11cb;
  box-shadow: 0 0 0 3px rgba(106, 17, 203, 0.1);
}

.select-arrow {
  position: absolute;
  right: 18px;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
  pointer-events: none;
}

/* 排行榜内容 */
.ranking-content {
  padding: 24px 32px;
}

/* 加载状态 */
.loading-state {
  padding: 60px 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.spinner {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: 4px solid rgba(106, 17, 203, 0.1);
  border-top: 4px solid #6a11cb;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 空状态 */
.empty-state {
  padding: 60px 0;
  text-align: center;
}

.empty-icon {
  font-size: 4rem;
  color: #cbd5e1;
  margin-bottom: 20px;
}

.empty-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: #475569;
  margin-bottom: 10px;
}

.empty-text {
  color: #94a3b8;
  max-width: 500px;
  margin: 0 auto;
}

/* 排行榜项目 */
.ranking-list {
  display: grid;
  gap: 16px;
}

.ranking-item {
  background: white;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  border: 1px solid #f1f5f9;
  position: relative;
}

.ranking-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 35px rgba(0, 0, 0, 0.1);
}

.item-header {
  display: flex;
  padding: 24px;
  cursor: pointer;
  align-items: center;
}

.rank-number {
  width: 60px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 16px;
  font-size: 1.8rem;
  font-weight: 800;
  color: white;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  margin-right: 20px;
  flex-shrink: 0;
}

.rank-1 .rank-number {
  background: linear-gradient(135deg, #FFD700, #FFA500);
}

.rank-2 .rank-number {
  background: linear-gradient(135deg, #C0C0C0, #A9A9A9);
}

.rank-3 .rank-number {
  background: linear-gradient(135deg, #CD7F32, #A0522D);
}

.rank-other .rank-number {
  background: linear-gradient(135deg, #6a11cb, #2575fc);
}

.item-info {
  flex: 1;
  min-width: 0;
}

.item-title {
  font-size: 1.4rem;
  font-weight: 700;
  color: #1e293b;
  line-height: 1.4;
  margin-bottom: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-meta {
  display: flex;
  gap: 16px;
  color: #64748b;
  font-size: 0.95rem;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.item-score {
  font-size: 1.8rem;
  font-weight: 800;
  color: #6a11cb;
  display: flex;
  align-items: flex-end;
  gap: 4px;
  white-space: nowrap;
  margin-left: 20px;
}

.score-label {
  font-size: 1rem;
  font-weight: 500;
  color: #94a3b8;
}

.toggle-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: #f1f5f9;
  color: #64748b;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-left: 16px;
  flex-shrink: 0;
}

.toggle-btn:hover {
  background: #e2e8f0;
}

.toggle-btn i {
  transition: transform 0.3s ease;
}

.toggle-btn.expanded i {
  transform: rotate(180deg);
}

.item-details {
  padding: 0 24px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.4s ease, padding 0.4s ease;
}

.item-details.expanded {
  padding: 0 24px 24px;
  max-height: 500px;
}

.item-description {
  color: #64748b;
  line-height: 1.6;
  margin-bottom: 20px;
}

.item-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 20px;
}

.tag {
  padding: 6px 14px;
  background: #f1f5f9;
  color: #475569;
  border-radius: 100px;
  font-size: 0.85rem;
  font-weight: 500;
  transition: all 0.2s ease;
}

.tag:hover {
  background: #e2e8f0;
  transform: translateY(-2px);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-top: 16px;
}

.stat-card {
  background: #f8fafc;
  border-radius: 14px;
  padding: 16px;
  display: flex;
  flex-direction: column;
}

.stat-label {
  color: #64748b;
  font-size: 0.9rem;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: #6a11cb;
}

/* 分页 */
.pagination-section {
  padding: 32px 0 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
}

.pagination-info {
  color: #64748b;
  font-weight: 500;
}

.pagination-controls {
  display: flex;
  gap: 8px;
}

.pagination-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  background: white;
  border: 1px solid #e2e8f0;
  color: #64748b;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
}

.pagination-btn:hover {
  background: #f1f5f9;
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
}

.pagination-btn.active {
  background: #6a11cb;
  border-color: #6a11cb;
  color: white;
  box-shadow: 0 4px 10px rgba(106, 17, 203, 0.2);
}

.pagination-btn.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}

/* 响应式设计 */
@media (max-width: 900px) {
  .header-content {
    flex-direction: column;
    text-align: center;
    gap: 20px;
  }
  
  .header-text {
    margin-bottom: 10px;
  }
}

@media (max-width: 768px) {
  .item-header {
    flex-wrap: wrap;
  }
  
  .item-score {
    margin-left: 0;
    margin-top: 12px;
    width: 100%;
  }
  
  .toggle-btn {
    position: absolute;
    right: 20px;
    top: 24px;
  }
}

@media (max-width: 600px) {
  .filters-container {
    flex-direction: column;
  }
  
  .pagination-section {
    flex-direction: column;
    align-items: center;
  }
  
  .header-section {
    padding: 24px;
  }
  
  .ranking-content {
    padding: 20px;
  }
  
  .item-header {
    padding: 20px;
  }
  
  .rank-number {
    width: 50px;
    height: 50px;
    font-size: 1.5rem;
  }
  
  .item-title {
    font-size: 1.2rem;
  }
}
</style>